from utils import *
import json
import friends
import information
import account
import datetime

"""
INFO_ID_BLABLA_list :
{ "ID_blabla" : "<fr>",
...
}

"""

INFO_ID_BLABLA = "blabla"
INFO_ID_BLABLA_list = INFO_ID_BLABLA + "/list"
INFO_ID_BLABLA_blabla_root = INFO_ID_BLABLA + "/blabla/"
INFO_ID_BLABLA_bla_root = INFO_ID_BLABLA + "/bla/"

class blabla:
    def __init__(self,owner=None,id="",title="",fr=None):
        self._owner = owner
        self._id = id
        self._title = title
        self._fr = fr
        
    def getTitle(self):
        return self._title
    
    def getId(self):
        return self._id
    
    def getFr(self):
        return self._fr
        
    def loads(self,JSONString):
        dicJson = json.loads(JSONString)
        #owner dont have to appear in data dump it is linked to info author
        #self._owner = friends.getGlobalAccount(dicJson['owner'])
        self._id = dicJson['id']
        self._title = dicJson['title']
        
    def dumps(self):
        dicJson = {}
        #owner dont have to appear in data dump it is linked to info author
        #dicJson["owner"] = self._owner.getID()
        dicJson["id"] = self._id
        dicJson["title"] = self._title
        return json.dumps(dicJson)

class bla:
    def __init__(self,owner=None,id=None,blablaid=None,content="",date=None):
        self._owner = owner
        self._id = id
        self._blablaid = blablaid
        self._content = content
        self._date = date
        
    def getId(self):
        return self._id

    def loads(self,JSONString):
        dicJson = json.loads(JSONString)
        #owner dont have to appear in data dump it is linked to info author
        #self._owner = friends.getGlobalAccount(dicJson['owner'])
        self._id = dicJson['id']
        self._blablaid = dicJson['blablaid']
        self._content = dicJson['content']
        self._date = datetime.datetime.strptime(dicJson['date'],"%Y-%m-%dT%H:%M:%S.%f")
        
    def dumps(self):
        dicJson = {}
        #owner dont have to appear in data dump it is linked to info author
        #dicJson["owner"] = self._owner.getID()
        dicJson["id"] = self._id
        dicJson["blablaid"] = self._blablaid
        dicJson["content"] = self._content
        dicJson["date"] = self._date.isoformat()
        return json.dumps(dicJson)

class blablaFactory:
    def __init__(self):
        return
    
    def createBlaBla(self,title,text,fr):
        """create a new own blabla thread
        title and text are strings, fr is friendsGroup object
        return id or None if error"""
        #create a new blabla
        myBlabla = blabla(account.getMyAccount(),genRandomId(),title,fr)
        information.setPersonalInformation(INFO_ID_BLABLA_blabla_root+myBlabla.getId(),myBlabla.getFr(),myBlabla.dumps())
        #create a new bla linked to blabla
        firstBla = bla(account.getMyAccount(),genRandomId(),myBlabla.getId(),text,datetime.datetime.now())
        information.setPersonalInformation(INFO_ID_BLABLA_bla_root+firstBla.getId(),myBlabla.getFr(),firstBla.dumps())
        #update the blabla list
        listInfo = information.getPersonalInformation(INFO_ID_BLABLA_list)
        if not listInfo: #in case first thread
            #TODO : migrate to plugin loading
            lists = "{}"
        else:
            lists = listInfo.getData()
        listjson = json.loads(lists)
        listjson[myBlabla.getId()] = fr.dumps()
        information.setPersonalInformation(INFO_ID_BLABLA_list,friends.FRIENDSGROUP_ALL,json.dumps(listjson))
    
    def getListBlaBlas(self):
        """return the list of blablas known"""
        #TODO : handle indirect fr grab
        #get blabla list of all friends
        blablaIDs = []
        for friend in friends.getFriendsPool().getAllFriends():
            tmplistInfo = information.getInformation(INFO_ID_BLABLA_list,friend)
            if tmplistInfo:
                print "list data : %s"%tmplistInfo.getData()
                tmpdicInfo = json.loads(tmplistInfo.getData())
                for id in tmpdicInfo:
                    fr = friends.friendsGroup([])
                    fr.loads(tmpdicInfo[id])
                    if fr.isMember(account.getMyAccount()):
                        blablaIDs.append((id,friend))
        #get blabla with which we have access
        blablas = []
        for id,f in blablaIDs:
            infoObj = information.getInformation(INFO_ID_BLABLA_blabla_root+id,f)
            if infoObj:
                b = blabla()
                b.loads(infoObj.getData())
                blablas.append(b)
        #return list
        return blablas
        